package com.kfm.spring.service.impl;

import com.kfm.spring.domain.Account;
import com.kfm.spring.mapper.AccountMapper;
import com.kfm.spring.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper mapper;

    @Override
    public List<Account> findAll() {
        return mapper.selectAllAccounts();
    }

    @Override
    @Transactional(readOnly = true)
    public Account findById(Integer id) {
        System.out.println("AccountServiceImpl.findById");
        return mapper.selectAccountById(id);
    }

    @Override
    @Transactional
    public void transfer(Integer fromId, Integer toId, Double amount) {
        System.out.println("AccountServiceImpl.transfer");
        Account from = mapper.selectAccountById(fromId);
        Account to = mapper.selectAccountById(toId);


        from.setMoney(from.getMoney() - amount);
        to.setMoney(to.getMoney() + amount);
        mapper.updateMoneyById(from);
        int i = 1 / 0;
        mapper.updateMoneyById(to);
    }
}
